<?php
class Notify
{
    public function pend($uid,$message)
    {
        $query = 'INSERT INTO `notify` SET `user_id`='.$uid.',`message`=\''.mysql_escape_string($message).'\'
ON DUPLICATE KEY UPDATE `message`=\''.mysql_escape_string($message).'\'';
        _database::query($query);
    }
    public static function send($uid,$message)
    {
        if(is_array($uid))
        {
            $uids = implode(',', $uid);
        }
        else if($uid) {$uids = $uid;}
        else return false;

		$random = rand();
		$params = array();
        $params['api_id'] = _settings::$api_settings['app_id'];
		$params['v'] = '2.0';
		$params['timestamp'] = time();
		$params['random'] = $random;
		$params['uids'] = $uids;
		$params['message'] = $message;
		$params['format'] = 'JSON';
		$params['method'] = 'secure.sendNotification';
		ksort($params);
		$params_ready = array();
		foreach ($params as $key=>$val)
			$params_ready[$key] = $key.'='.$val;
            $sig = md5(implode("",$params_ready)._settings::$api_settings['secret']);
		//echo implode("",$params_ready).API_SECRET."<br>";
		$params_ready['sig'] = 'sig='.$sig;
		$params_ready['message'] = 'message='.urlencode($message);
		$url = 'http://api.vkontakte.ru/api.php?'.implode('&',$params_ready);
		ob_start();

		$ch = curl_init($url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);
		curl_exec($ch);
		curl_close($ch);
		$result = ob_get_contents();
		ob_end_clean();
        $r = @json_decode($result);
        if($r && is_object($r) && isset($r->response))
		return $r->response;
        else
        return $result;
    }
}
?>
